package com.ccb.ha.zhxf.controller;


import com.ccb.ha.zhxf.entity.User;
import com.ccb.ha.zhxf.result.Result;
import com.ccb.ha.zhxf.result.ResultEnum;
import com.ccb.ha.zhxf.result.ResultUtil;
import com.ccb.ha.zhxf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;
    private int code;
    private String msg;
    @RequestMapping(value = "/findAll", method = RequestMethod.POST)
    @ResponseBody

    public Result findAll (){

        List<User> users = userService.findAll();
        if (users != null){
            return ResultUtil.success(users);
        }else {

            return ResultUtil.error(ResultEnum.DATA_IS_NULL.getCode(), ResultEnum.DATA_IS_NULL.getMsg());
        }
    }

    @RequestMapping(value = "/findById", method = RequestMethod.POST)
    @ResponseBody
    public Result findById(int mechanism_id){

        List<User> users = userService.findById(mechanism_id);
        if (users !=null){
            return ResultUtil.success(users);
        }else {

            return ResultUtil.error(ResultEnum.DATA_IS_NULL.getCode(), ResultEnum.DATA_IS_NULL.getMsg());
        }
    }

    @RequestMapping(value = "/InsertUser", method = RequestMethod.POST)
    @ResponseBody
    public Result InsertUser(@RequestParam("id") int id,
                             @RequestParam("name") String name,
                             @RequestParam("user_name") String user_name,
                             @RequestParam("user_pwd") String user_pwd,
                             @RequestParam("mechanism_id") int mechanism_id,
                             @RequestParam("is_delete") int is_delete){
        User users = new User();
        users.setId(id);
        users.setName(name);
        users.setUser_name(user_name);
        users.setUser_pwd(user_pwd);
        users.setMechanism_id(mechanism_id);
        users.setIs_delete(is_delete);
        int result = userService.InsertUser(users);
        if (result == 1){

            return ResultUtil.success(result);
        }else {

            return ResultUtil.error(ResultEnum.USER_IS_EXISTS.getCode(), ResultEnum.USER_IS_EXISTS.getMsg());
        }
    }

    @RequestMapping(value = "/UpdateUser2", method = RequestMethod.POST)
    @ResponseBody
    public Result UpdateUser2(@RequestParam("id") int id,
                             @RequestParam("name") String name,
                             @RequestParam("user_name") String user_name,
                             @RequestParam("user_pwd") String user_pwd,
                             @RequestParam("mechanism_id") int mechanism_id,
                             @RequestParam("is_delete") int is_delete){
        Map users = new HashMap();
        users.put("id", id);
        users.put("name", name);
        users.put("user_name", user_name);
        users.put("user_pwd", user_pwd);
        users.put("mechanism_id", mechanism_id);
        int result = userService.updateUser(users);
        if (result == 1){

            return ResultUtil.success(result);
        }else {

            return ResultUtil.error(ResultEnum.USER_IS_EXISTS.getCode(), ResultEnum.USER_IS_EXISTS.getMsg());
        }
    }

    @RequestMapping(value = "/UpdateUser", method = RequestMethod.POST)
    @ResponseBody
    public Result UpdateUser(@RequestParam("id") int id,
                             @RequestParam("name") String name,
                             @RequestParam("user_name") String user_name,
                             @RequestParam("user_pwd") String user_pwd,
                             @RequestParam("mechanism_id") int mechanism_id,
                             @RequestParam("is_delete") int is_delete){
        User users = new User();
        users.setId(id);
        users.setName(name);
        users.setUser_name(user_name);
        users.setUser_pwd(user_pwd);
        users.setMechanism_id(mechanism_id);
        users.setIs_delete(is_delete);
        int result = userService.updateUser(users);
        if (result == 1){

            return ResultUtil.success(result);
        }else {

            return ResultUtil.error(ResultEnum.USER_IS_EXISTS.getCode(), ResultEnum.USER_IS_EXISTS.getMsg());
        }
    }

    @RequestMapping(value = "/deleteUser", method = RequestMethod.POST)
    @ResponseBody
    public Result deleteUser(@RequestParam("id") int id){

        int result = userService.deleteUser(id);
        if (result == 1){

            return ResultUtil.success(result);
        }else {

            return ResultUtil.error(ResultEnum.USER_IS_EXISTS.getCode(), ResultEnum.USER_IS_EXISTS.getMsg());
        }
    }
}
